﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIGame : MonoBehaviour 
{
    private Button btn_Pause;
    private Text text_Score;
    private Text text_Diamond;   
   
    public Sprite[] sprite_Pause;

   
    private void Awake()
    {
        Init();       
    }

    /// <summary>
    /// 初始化UIGame
    /// </summary>
    private void Init()
    {
        EventCenter.AddListener(CL.EventType.ShowUIGame, ShowUIGame);
        EventCenter.AddListener(CL.EventType.HideUIGame, HideUIGame);
        EventCenter.AddListener(CL.EventType.UpdateScoreOnUIGame, UpdateScoreOnUIGame);
        EventCenter.AddListener(CL.EventType.UpdateDiamondOnUIGame, UpdateDiamondOnUIGame);


        btn_Pause = transform.Find("Top/Btn_Pause").GetComponent<Button>();        
        btn_Pause.onClick.AddListener(Btn_Pause);
        //每次重新进入游戏不仅重新初始化isPause = false，还要初始化按钮图片
        btn_Pause.gameObject.GetComponent<Image>().sprite = sprite_Pause[0];
        text_Score = transform.Find("Top/Text_Score").GetComponent<Text>();
        text_Diamond = transform.Find("Top/Diamond/Text").GetComponent<Text>();


    }
    

    private void OnDestroy()
    {
        EventCenter.RemoveListener(CL.EventType.ShowUIGame, ShowUIGame);
        EventCenter.RemoveListener(CL.EventType.HideUIGame, HideUIGame);
        EventCenter.RemoveListener(CL.EventType.UpdateScoreOnUIGame, UpdateScoreOnUIGame);
        EventCenter.RemoveListener(CL.EventType.UpdateDiamondOnUIGame, UpdateDiamondOnUIGame);
    }

    /// <summary>
    /// 暂停、继续按钮点击
    /// </summary>
    private void Btn_Pause()
    {
        AudioManager.Instance.PlaySound("button");
        if (GameManager.Instance.isPause)
        {
            btn_Pause.gameObject.GetComponent<Image>().sprite = sprite_Pause[0];
            GameManager.Instance.OnGameContinue();
        }
        else
        {
            btn_Pause.gameObject.GetComponent<Image>().sprite = sprite_Pause[1];
            GameManager.Instance.OnGamePause();            
        }
    }

    /// <summary>
    /// 显示游戏界面
    /// </summary>
    private void ShowUIGame()
    {
        gameObject.SetActive(true);    
    }
    private void HideUIGame()
    {
        gameObject.SetActive(false);  
    }




    /// <summary>
    /// 更新UIGame界面分数显示（因为跟随游戏进程分数一直增加，所以写成事件监听）
    /// </summary>
    private void UpdateScoreOnUIGame()
    {
        text_Score.text = DataManager.Instance.mGameData.currentScore.ToString();        
    }
    /// <summary>
    /// 更新游戏中当前获得的钻石数量显示
    /// </summary>
    private void UpdateDiamondOnUIGame()
    {
        text_Diamond.text = DataManager.Instance.mGameData.currentDiamond.ToString();
    }

}
